<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of flicker
 *
 * @author sifat
 * 
 * @doc http://www.flickr.com/services/api/flickr.photos.search.html
 * 
 * example search url http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=7e66d854be47bfd2ea892c53292efaca&text=bangladesh&format=php_serial
 * api key: 7e66d854be47bfd2ea892c53292efaca
 * image url: http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}_[mstzb].jpg
 */

class flickr extends BaseClass{
    //put your code here
    public $url='http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=7e66d854be47bfd2ea892c53292efaca&format=php_serial';
    function __construct($data, $EE) {
        parent::__construct($data, $EE);
        
        $this->options=$this->apiOptions('flickr');
        $this->fields=$this->getFields('flickr');
        $this->generateUrl();
    }
    
    function generateUrl(){
        $this->url .='&text='.urlencode(implode("+", $this->keyword));
        if($this->options['tags']){
            $tags=$this->options['tags'];
            $tags=str_replace(", ", ',', $tags);
            $this->url .='&tags='.urlencode($tags);
        }
    }
    
    function searchReasults(){
        return unserialize(file_get_contents($this->url));
    }
    
    function getData() {
        
        if(!$this->options || !$this->fields){
            return array();
        }
        
        $this->formateData();
        
        if(empty ($this->insertData)){
            return array();
        }
        
        return $this->insertData;
    }
    
    function formateData(){
        $searchReasults=$this->searchReasults();
        
        if(empty ($searchReasults['photos']['photo'])){
            return false;
        }
        
        $i=0;
        foreach ($searchReasults['photos']['photo'] as $result){
            foreach ($this->fields as $key=>$value){
                if (substr($key, 0, strpos($key, '_'))!='fmt'){
                    if(isset ($this->fields['title'])){
                        if($this->fields['title']==1 && $key=='title'){
                            $this->insertData[$i]['field_id_'.$value]=$result['title'];
                        }
                    }
                    
                    if($key!='title'){
                        $this->insertData[$i]['field_id_'.$value]=$this->getImageUrl($result);
                    }
                    
                    
                    $this->insertData[$i]['field_ft_'.$value]=  $this->fields['fmt_'.$key];
                    $this->insertData[$i]['title']='flickr-'.implode("&", $this->keyword) . '-at-' . date('Y-m-d', time()) .'-'. rand(0, 9999);
                    $this->insertData[$i]['entry_date']=time();
                }
            }
            $i++;
        }
    }
    
    function getImageUrl($result){
        return 'http://farm'.$result['farm'].'.static.flickr.com/'.$result['server'].'/'.$result['id'].'_'.$result['secret'].'_'.$this->options['image_size'].'.jpg';
    }
}

?>
